40923235 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首頁
  • HW1
    • UNIT 1
      • Strategies for Learning學習策略
      • What You Need to Know About the Course您需要了解的課程內容
      • About these Materials 關於這些材料
      • Metacognition元認知
      • Metacognition in Action行動中的元認知
    • UNIT2
      • Introduction簡介
      • Functions of Computers  Input  Output  Storage  and Processing計算機功能  輸入  輸出  存儲和處理
      • Hardware硬件
      • Software軟件
      • Fireware固件
      • Componentization  Standardization組件化  標準化
      • Connection Interfaces and Cables連接接口和電纜
  • HW2
  • HW3
    • Chacrater input字符輸入
      • input strings types int輸入字符串類型int
      • Discussion討論區-1
      • User input in Python使用Python的用戶輸入
      • Manipulating strings a few ways處理字符串幾種方法
    • Odd Or Even奇數或偶數
      • Exercise 2 and solution練習2和解決方案
      • Discussion討論區-2
      • Modular arithmetic the modulus operator模塊化算術模運算符
      • Conditionals有條件的
      • Checking for equality and comparators in general檢查是否相等以及一般的比較器
    • List Less Than Ten列出少於十
      • Exercise 3 and Solution練習3和解決方案
      • Discussion討論區-3
      • Lists清單
      • More Conditionals更多條件
  • 心得
Conditionals有條件的 << Previous Next >> List Less Than Ten列出少於十

Checking for equality and comparators in general檢查是否相等以及一般的比較器

A fundamental thing you want to do with your program is check whether some number is equal to another. Say the user tells you how many questions they answered incorrectly on a practice exam, and depending on the number of correctly-answered questions, you can suggest a specific course of action. For integers, strings, floats, and many other variable types, this is done with a simple syntax: ==. To explicitly check inequality, use !=.

if a == 3:

print("the variable has the value 3")

elif a != 3:

print("the variable does not have the value 3")

Notice how in this example, the condition is redundant. In the first condition we are checking whether the variable a has the value 3 and in the second, we are checking whether a does NOT have the value 3. However, if the first condition is not true (a is in fact not 3), then the second condition is by definition true. So a more efficient way to write the above conditional is like this:

if a == 3:

print("the variable has the value 3")

else:

print("the variable does not have the value 3")

The same equality / inequality comparisons work for strings.

您要對程序執行的基本操作是檢查某個數字是否等於另一個數字。假設用戶告訴您在練習考試中有多少個錯誤回答的問題,並且根據正確回答的問題數,您可以提出具體的操作建議。對於整數,字符串,浮點數和許多其他變量類型,這是通過簡單的語法完成的:==。要顯式檢查不平等,請使用!=。

  
if a == 3: 
  print("the variable has the value 3")
elif a != 3:
  print("the variable does not have the value 3")

請注意,在此示例中,條件是多餘的。在第一個條件中,我們正在檢查變量是否a具有值3,在第二個條件中,我們正在檢查變量是否a不具有值3。但是,如果第一個條件不成立(a實際上不是3),則第二個條件根據定義,條件是正確的。因此,編寫上述條件的更有效方法如下:

if a == 3: 
  print("the variable has the value 3")
else:
  print("the variable does not have the value 3")

相同的相等/不相等比較適用於字符串。


Conditionals有條件的 << Previous Next >> List Less Than Ten列出少於十

Copyright © All rights reserved | This template is made with by Colorlib